package com.cj.webapp.account.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cj.webapp.common.sign.SignVerifyHandle;
import com.cj.webapp.common.utils.HFJsonUtil;
import com.cj.webapp.common.utils.http.HttpsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * 我来解析
 * Created by dell on 2017/12/25.
 */

public class MyServlet extends HttpServlet {
    private static final Logger logger = LoggerFactory.getLogger(MyServlet.class);
    private static SignVerifyHandle signVerifyHandle=new SignVerifyHandle();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        HttpSession session = request.getSession();

        BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(),"UTF-8"));
        String line = null;
        StringBuilder sb = new StringBuilder();
        while((line = br.readLine())!=null){
            sb.append(line);
        }
        // 请求参数
        //String requestJson = URLDecoder.decode(sb.toString(), "UTF-8");
        String requestJson =sb.toString();
        try {
            Map<String, String> jsonToMap = HFJsonUtil.getJsonToMap(requestJson);
            //JSONObject jsonObject=JSON.parseObject(requestJson);
            Map<String,String> mapRes=new HashMap<>();
            if(signVerifyHandle.verifySign("00001",jsonToMap.get("rspData"),jsonToMap.get("signData"))){
                mapRes= HFJsonUtil.getJsonToMap(jsonToMap.get("rspData"));
            }
            logger.info("接收的数据"+requestJson+"==解析成的json对象"+jsonToMap+"内容："+mapRes);
        }catch (Exception e){
            e.printStackTrace();
            logger.info("对不起，报错啦！！接收的数据"+requestJson);
        }

        response.getWriter().print("你好");


    }
    
}
